*** to be run after 1_Table_2\4_wage_equation


clear
version 13
set more off 
cap log close

*just group 1
local gruppo = 1

log  using  ".../A_5_`gruppo'_cap_3_05Sep2020.smcl", replace

use .../left_cens_spell_10sample_10never_type_`gruppo'_rev1_04Aug2020.dta, clear

*** add variable to the next line if somewhere in the dofile a vairable is already existingg
*** remove variable if instead the drop command gives error that the variable does not exist
*drop  tax_naer_exp tax_wage_exp average_random_wage random_wage_0  tax_random_wage_0 convexity_diff convexity_naer convexity_wage average_random_naer random_naer_0 tax_random_naer_0


rename predlnwagelambda pred_lnwage  
rename predlnnaeringlambda pred_lnnaer  
rename lambdanear lambda_naer
rename lambdawage lambda_wage 
rename millsnaer mills_naer
rename millswage mills_wage

*** generating the (heteroskedastic) variance  of logwage and lognaering
gen var_wage=h_hat_wage
gen var_naer=h_hat_naer

***tax convexity parameter
capture drop __000000 __000001 __000002 __000003 __000004 __000005 __000006 __00000A __000008 __000009 __00000B __00000C
set more off 
set seed 210385
global draws 200

 set more off
 forvalues r=1/$draws {
 gen double random_wage_`r'=rnormal(pred_lnwage,sqrt(var_wage))
 replace random_wage_`r'=exp(random_wage_`r')
 }
 egen double random_wage_0=rowmean(random_wage_1-random_wage_$draws)

 

 set more off

forvalues r=0/$draws {
foreach inc in random_wage_`r'  {
gen tax_`inc'=0
}
}

*tax liability







foreach y of num 1993/2011 {
 forvalues r=0/$draws {
   foreach inc in random_wage_`r'  {
 
      if `y'==1993 {
           if `inc'==random_wage_`r' {
	   tempvar gross gross1 alm0 alm1 xss xkom  xsta name b b1 z dedu k
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	     gen double  `k'=capital_income
	    
	   // Deductions 
           replace `dedu' = min(27600, max((`z'+`b')*.2,3300))
              
           // Tax base and calculated taxes
           replace `gross' = `z'+`b'  	if year==`y'														     // for business using here net naering b
           replace `gross1' = `z'+`b1'		if year==`y'															 // for business using here imputed labor b1
           replace `ordinary0' = max(`gross' + `k' - `dedu', 0)   	if year==`y'                      // k is capital, geo is geografical alowance           
           replace `ordinary1' = max(`ordinary0' - 22200 , 0) if year==`y'

           replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross'>17000) , 0.25*[`gross'-17000]) , 0) 	if year==`y'         // for wages and b business (net_naering)

           replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
           replace `surtax' = max(`gross1'-204000,0)*.095 + max(`gross1'-247000,0)*.042 	if year==`y'               // surtax, use imputed personal for business
                                        
           // Total income tax 
           replace tax_`inc' = `sic' + `flat' + `surtax'  	if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1994 {
           if `inc'==random_wage_`r' {
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	   // Deductions 
           replace `dedu' = min(28100, max((`z'+`b')*.2,3400))
                   
           // Tax base and calculated taxes
           replace `gross' = `z'+`b'  	if year==`y'														     // for business using here net naering b
           replace `gross1' = `z'+`b1'		if year==`y'															 // for business using here imputed labor b1
           replace `ordinary0' = max(`gross' + `k' - `dedu', 0)   	if year==`y'                      // k is capital, geo is geografical alowance           
           replace `ordinary1' = max(`ordinary0' - 22600 , 0) if year==`y'

           replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross'>17000) , 0.25*[`gross'-17000]) , 0) 	if year==`y'         // for wages and b business (net_naering)

           replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
           replace `surtax' = max(`gross1'-208000,0)*.095 + max(`gross1'-252000,0)*.042 	if year==`y'               // surtax, use imputed personal for business
                    
           // Total income tax 
           replace tax_`inc' = `sic' + `flat' + `surtax'  	if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'	
	     }
      }
      
      if `y'==1995 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	// Deductions 
	replace `dedu' = min(28700, max((`z'+`b')*.2,3500)) 
	    
	// Tax base and calculated taxes
	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu', 0) if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 22600 , 0) if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0) if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-212000,0)*.095 + max(`gross1'-239000,0)*.042 if year==`y'                 // surtax, use imputed personal for business
			    
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'  if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'	
	  }
      }
      
      if `y'==1996 {
           if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	// Deductions 

	replace `dedu' = min(29800, max((`z'+`b')*.2,3600)) if year==`y'
			 
	// Tax base and calculated taxes
	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 23300 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-220500,0)*.095 + max(`gross1'-248500,0)*.042  if year==`y'                 // surtax, use imputed personal for business

			     
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1997 {
           if `inc'==random_wage_`r' {
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	// Deductions 
	replace `dedu' = min(31300, max((`z'+`b')*.2,3700)) if year==`y'
			   
	// Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 24100 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-233000,0)*.095 + max(`gross1'-262500,0)*.042  if year==`y'                 // surtax, use imputed personal for business


	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1998 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	// Deductions
	replace `dedu' = min(32600, max((`z'+`b')*.2,3700)) if year==`y'
	 
			   
	// Tax base and calculated taxes


	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 25000 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-248000,0)*.095 + max(`gross1'-272000,0)*.042  if year==`y'                 // surtax, use imputed personal for business


	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'			    
			    

	  }
      }
      
      if `y'==1999 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	// Deductions 
	replace `dedu' = min(34900, max((`z'+`b')*.21,3900)) if year==`y'

			   
	// Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 26300 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-269100,0)*.135  if year==`y'                 // surtax, use imputed personal for business

			    
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'			    

	  }
      }
      
      if `y'==2000 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(36600, max((`z'+`b')*.21,30600)) if year==`y'
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 27700 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-277800,0)*.135 + max(`gross1'-762700,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'

	  }
      }
      
      if `y'==2001 {
          if `inc'==random_wage_`r' {
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	replace `dedu' = min(40300, max((`z'+`b')*.21,31800)) if year==`y'

			 
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 28800 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-289000,0)*.135 + max(`gross1'-793200,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2002 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(43000, max((`z'+`b')*.21,31800)) if year==`y'

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 30100 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-320000,0)*.135 + max(`gross1'-830000,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2003 {
          if `inc'==random_wage_`r' {
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(45700, max((`z'+`b')*.21,31800)) if year==`y'

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 31600 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-340700,0)*.135 + max(`gross1'-872000,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2004 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(45700, max((`z'+`b')*.24,31800)) if year==`y'
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 32900 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-354300,0)*.135 + max(`gross1'-906900,0)*.06  if year==`y'                 // surtax, use imputed personal for business

			    
	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2005 {
          if `inc'==random_wage_`r' {
              gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(57400, max((`z'+`b')*.24,31800)) if year==`y'

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 34200 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>29600) , 0.25*[`gross1'-29600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-381000,0)*.12 + max(`gross1'-800000,0)*.035  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2006 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	replace `dedu' = min(61100, max((`z'+`b')*.24,31800)) if year==`y'

			  
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 35400 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>29600) , 0.25*[`gross1'-29600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-394000,0)*.09 + max(`gross1'-750000,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2007 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions
	replace `dedu' = min(63800, max((`z'+`b')*.36,31800)) if year==`y'
	 
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 37000 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.2845  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-400000,0)*.09 + max(`gross1'-650000,0)*.03  if year==`y'                 // surtax, use imputed personal for business


	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2008 {
          if `inc'==random_wage_`r' {
              gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(67000, max((`z'+`b')*.36,31800)) if year==`y'

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 38850 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-420000,0)*.09 + max(`gross1'-682500,0)*.03  if year==`y'                 // surtax, use imputed personal for business


	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2009 {
          if `inc'==random_wage_`r' {
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(70350, max((`z'+`b')*.36,31800)) if year==`y'

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 40800 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-441000,0)*.09 + max(`gross1'-716600,0)*.03  if year==`y'                 // surtax, use imputed personal for business

			 
	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2010 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(72800, max((`z'+`b')*.36,31800)) if year==`y'
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 42210 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-456400,0)*.09 + max(`gross1'-741700,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2011 {
          if `inc'==random_wage_`r' {
             gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = `inc'
            gen double   `b'= 0
            gen double   `b1'= `b'*1
	    gen double   `dedu' = 0
	    
	*-* Deductions 
	replace `dedu' = min(75150, max((`z'+`b')*.36,31800)) if year==`y'
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital, geo is geografical alowance           
	replace `ordinary1' = max(`ordinary0' - 43600 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-471200,0)*.09 + max(`gross1'-765800,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      
   }
}
}

egen tax_wage_exp = rowmean(tax_random_wage_1-tax_random_wage_$draws ) 
gen average_random_wage=random_wage_0- tax_random_wage_0
replace average_random_wage=random_wage_0- tax_random_wage_0 + capital_income if capital_income>0
gen convexity_wage= (ln(tax_wage_exp/tax_random_wage_0))*(tax_random_wage_0/average_random_wage)
drop random_wage_1-random_wage_200
drop tax_random_wage_1- tax_random_wage_200

set seed 210385
global draws 200

forvalues r=1/$draws {
 gen double random_naer_`r'=rnormal(pred_lnnaer,sqrt(var_naer))
 replace random_naer_`r'=exp(random_naer_`r')
 }
 egen double random_naer_0=rowmean(random_naer_1-random_naer_$draws)
 
forvalues r=0/$draws {
foreach inc in random_naer_`r'  {
gen tax_`inc'=0
}
}

foreach y of num 1993/2011 {
forvalues r=0/$draws {
foreach inc in  random_naer_`r' {
      if `y'==1993 {

	  if `inc'==random_naer_`r' {
	   tempvar gross gross1 alm0 alm1 xss xkom  xsta name b b1 z dedu k
            gen double `gross' = 0
            gen double `gross1' = 0
            gen double   `ordinary0' = 0
            gen double   `ordinary1' = 0
            gen double    `sic' = 0
            gen double   `flat' = 0
            gen double   `surtax' = 0
	    gen double   `z' = 0
            gen double   `b'= `inc'
            gen double   `b1'= `b'*0.963 
	    gen double   `dedu' = 0
	    gen double  `k'=capital_income
	  
	  
	  // Deductions 
           replace `dedu' = 0
              
           // Tax base and calculated taxes
           replace `gross' = `z'+`b'  	if year==`y'														     // for business using here net naering b
           replace `gross1' = `z'+`b1'		if year==`y'															 // for business using here imputed labor b1
           replace `ordinary0' = max(`gross' + `k' - `dedu', 0)   	if year==`y'                      // k is capital,             
           replace `ordinary1' = max(`ordinary0' - 22200 , 0) if year==`y'

           replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross'>17000) , 0.25*[`gross'-17000]) , 0) 	if year==`y'         // for wages and b business (net_naering)

           replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
           replace `surtax' = max(`gross1'-204000,0)*.095 + max(`gross1'-247000,0)*.042 	if year==`y'               // surtax, use imputed personal for business
                    
                    
           // Total income tax 
          replace tax_`inc' = `sic' + `flat' + `surtax'  	if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1994 {
 	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	  // Deductions 
          replace `dedu' = 0
	  
          replace `gross' = `z'+`b'  	if year==`y'														     // for business using here net naering b
          replace `gross1' = `z'+`b1'		if year==`y'															 // for business using here imputed labor b1
          replace `ordinary0' = max(`gross' + `k' - `dedu', 0)   	if year==`y'                      // k is capital,             
          replace `ordinary1' = max(`ordinary0' - 22600 , 0) if year==`y'

          replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross'>17000) , 0.25*[`gross'-17000]) , 0) 	if year==`y'         // for wages and b business (net_naering)

          replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
          replace `surtax' = max(`gross1'-208000,0)*.095 + max(`gross1'-252000,0)*.042 	if year==`y'               // surtax, use imputed personal for business
                    
           // Total income tax 
          replace tax_`inc' = `sic' + `flat' + `surtax'  	if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1995 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	// Deductions 
	replace `dedu' = 0
		    
	// Tax base and calculated taxes
	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu', 0) if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 22600 , 0) if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0) if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28 if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-212000,0)*.095 + max(`gross1'-239000,0)*.042 if year==`y'                 // surtax, use imputed personal for business
			    
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'  if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1996 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	// Deductions 

	replace `dedu' = 0
			 
	// Tax base and calculated taxes
	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 23300 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-220500,0)*.095 + max(`gross1'-248500,0)*.042  if year==`y'                 // surtax, use imputed personal for business

			     
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1997 {
 
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	// Deductions 
	replace `dedu' = 0
			   
	// Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 24100 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-233000,0)*.095 + max(`gross1'-262500,0)*.042  if year==`y'                 // surtax, use imputed personal for business


	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==1998 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	// Deductions
	replace `dedu' = 0
	 
			   
	// Tax base and calculated taxes


	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 25000 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-248000,0)*.095 + max(`gross1'-272000,0)*.042  if year==`y'                 // surtax, use imputed personal for business


	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'			    
			    

	  }
      }
      
      if `y'==1999 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	// Deductions 
	replace `dedu' = 0

			   
	// Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 26300 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>17000) , 0.25*[`gross1'-17000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-269100,0)*.135  if year==`y'                 // surtax, use imputed personal for business

			    
	// Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'			    

	  }
      }
      
      if `y'==2000 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 27700 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-277800,0)*.135 + max(`gross1'-762700,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'

	  }
      }
      
      if `y'==2001 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	replace `dedu' = 0

			 
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 28800 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>22200) , 0.25*[`gross1'-22200]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-289000,0)*.135 + max(`gross1'-793200,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2002 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 30100 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-320000,0)*.135 + max(`gross1'-830000,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2003 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 31600 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-340700,0)*.135 + max(`gross1'-872000,0)*.06  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2004 {

	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 32900 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>23000) , 0.25*[`gross1'-23000]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-354300,0)*.135 + max(`gross1'-906900,0)*.06  if year==`y'                 // surtax, use imputed personal for business

			    
	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2005 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 34200 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>29600) , 0.25*[`gross1'-29600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-381000,0)*.12 + max(`gross1'-800000,0)*.035  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2006 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	replace `dedu' = 0

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,            
	replace `ordinary1' = max(`ordinary0' - 35400 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>29600) , 0.25*[`gross1'-29600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-394000,0)*.09 + max(`gross1'-750000,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2007 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions
	replace `dedu' = 0
	 
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,            
	replace `ordinary1' = max(`ordinary0' - 37000 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.107*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.2845  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-400000,0)*.09 + max(`gross1'-650000,0)*.03  if year==`y'                 // surtax, use imputed personal for business


	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2008 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 38850 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-420000,0)*.09 + max(`gross1'-682500,0)*.03  if year==`y'                 // surtax, use imputed personal for business


	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2009 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0

			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 40800 , 0)  if year==`y'


	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-441000,0)*.09 + max(`gross1'-716600,0)*.03  if year==`y'                 // surtax, use imputed personal for business

			 
	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2010 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 42210 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-456400,0)*.09 + max(`gross1'-741700,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      if `y'==2011 {
	  if `inc'==random_naer_`r' {
	  gen double  `gross' = 0
          gen double `gross1' = 0
          gen double   `ordinary0' = 0
          gen double   `ordinary1' = 0
          gen double    `sic' = 0
          gen double   `flat' = 0
          gen double   `surtax' = 0
	  gen double   `z' = 0
          gen double   `b'= `inc'
          gen double   `b1'= `b'*0.963
	  gen double   `dedu' = 0
	  
	*-* Deductions 
	replace `dedu' = 0
			    
	*-* Tax base and calculated taxes

	replace `gross' = `z'+`b'  															     // for business using here net naering b
	replace `gross1' = `z'+`b1'																 // for business using here imputed labor b1
	replace `ordinary0' = max(`gross' + `k' - `dedu' , 0)  if year==`y'                         // k is capital,             
	replace `ordinary1' = max(`ordinary0' - 43600 , 0)  if year==`y'

	replace `sic' = max(min([.078*`z'+.11*`b1']*(`gross1'>39600) , 0.25*[`gross1'-39600]) , 0)  if year==`y'         // for wages and b business (net_naering)
	replace `flat' = `ordinary1'*.28  if year==`y'															  // for business use net naering, flat tax
	replace `surtax' = max(`gross1'-471200,0)*.09 + max(`gross1'-765800,0)*.03  if year==`y'                 // surtax, use imputed personal for business

	*-* Total income tax 
	replace tax_`inc' = `sic' + `flat' + `surtax'   if year==`y'
           drop  `gross' `gross1' `ordinary0' `ordinary1' `sic' `flat' `surtax'  `z'   `b'   `b1' `dedu'
	  }
      }
      
      
   }
} 
} 



egen tax_naer_exp = rowmean(tax_random_naer_1-tax_random_naer_$draws )
***** rifare con correzione per average_randiom naer
gen average_random_naer=random_naer_0- tax_random_naer_0
replace average_random_naer=random_naer_0- tax_random_naer_0 + capital_income if capital_income>0
gen convexity_naer= (ln(tax_naer_exp/tax_random_naer_0))*(tax_random_naer_0/average_random_naer)

drop random_naer_1-random_naer_200
drop tax_random_naer_1-tax_random_naer_200
gen convexity_diff=convexity_naer-convexity_wage
save .../left_cens_spell_10sample_10never_type_`gruppo'_rev1_cap_3_Sep2020.dta,replace
log close
